import { v4 as uuid } from 'uuid';
import insert from './atom/db/insert';

declare const action_id: string;
declare const session_id: string;
declare const spaceid: string;
declare const send_msg: any;

interface Message {
	exam_name: string;
	start_time: string;
	end_time: string;
	pm_start_time: string;
	am_start_time: string;
	creater: string;
}

async function code() {
	const msg = {
		pname: 'BM0010002',
		setinfo: '',
		spaceid,
		systemid: '12'
	};
	return (await send_msg('dataservice.finddoccode', msg)).result;
}

async function atom(msg: Message) {
	const systemid = '5981';
	const { prepare, exec } = insert(action_id, session_id, spaceid, systemid);
	// 调用编码服务
	const exam_id = await code();
	const start_date = new Date(msg.start_time).getTime();
	const finish_date = new Date(msg.end_time).getTime();
	const days = (finish_date - start_date) / 1000 / 60 / 60 / 24 + 1;
	const rowdata = { _id: uuid(), exam_name: msg.exam_name, exam_id, start_date, finish_date, cteate_time: new Date().getTime(), creater: msg.creater, campus_id: '', campus_name: '', fzno: 0, days, default_val: '否', am_start_time: msg.am_start_time, pm_start_time: msg.pm_start_time };
	prepare('exam', [rowdata]);
	const ret = await exec();
	if (ret.code === 1) {
		return 'success';
	} else {
		return 'fail';
	}
}
